import archive.Archiver;
import archive.zip.Ziper;
import com.jfoenix.controls.*;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import nodes.NodeHandler;
import nodes.progress.ArchiveProgress;

import java.io.File;
import java.util.List;
import java.util.Objects;

public class ArchiveMainWidget extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(Objects.requireNonNull(getClass().getClassLoader().getResource("main_view.fxml")));

        JFXComboBox<String> archiveType = (JFXComboBox<String>) root.lookup("#archive-type");
        archiveType.getItems().add("zip");
        archiveType.getItems().add("gz");
        archiveType.setPromptText("压缩方式");
        archiveType.getSelectionModel().select(0);
        Label archiveTypeSuffix = (Label) root.lookup("#archive-type-suffix");

        JFXCheckBox ifLevel = (JFXCheckBox) root.lookup("#if-level");
        JFXCheckBox ifComment = (JFXCheckBox) root.lookup("#if-comment");
        JFXSlider levelSlider = (JFXSlider) root.lookup("#level");
        JFXTextField commentField = (JFXTextField) root.lookup("#comment");

        FileChooser fileChooser = new FileChooser();
        DirectoryChooser directoryChooser = new DirectoryChooser();
        ImageView file = (ImageView) root.lookup("#file");
        ImageView dir = (ImageView) root.lookup("#dir");
        JFXTextField srcs = (JFXTextField) root.lookup("#srcs");
        ImageView desDir = (ImageView) root.lookup("#des-dir");
        JFXTextField desPath = (JFXTextField) root.lookup("#des-path");

        JFXTextField archiveName = (JFXTextField) root.lookup("#archive-name");
        JFXButton startArchive = (JFXButton) root.lookup("#start-archive");
        JFXTextField comment = (JFXTextField) root.lookup("#comment");
        JFXSlider archiveLevel = (JFXSlider) root.lookup("#level");
        ImageView resetArchive = (ImageView) root.lookup("#reset-archive");

        JFXProgressBar archiveProgress = (JFXProgressBar) root.lookup("#archive-progress");
        JFXProgressBar unarchiveProgress = (JFXProgressBar) root.lookup("#unarchive-progress");

        ImageView archivedFileIcon = (ImageView) root.lookup("#archive-file");
        JFXTextField zipSrc = (JFXTextField) root.lookup("#zip-src");
        ImageView unarchiveDirIcon = (ImageView) root.lookup("#unarchive-dir");
        JFXTextField unarchiveDesDir = (JFXTextField) root.lookup("#unarchive-des-path");

        JFXButton startUnarchive = (JFXButton) root.lookup("#start-unarchive");





        // 选择不同的压缩方式时，修改压缩文件的后缀名
        archiveType.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> archiveTypeSuffix.setText("." + newValue));

        ifLevel.selectedProperty().addListener((observable, oldValue, newValue) -> {
            // 如果复选框选中，则解除对滚动条的滚动限制
            if(newValue) {
                levelSlider.setDisable(false);
            }
            else {
                levelSlider.setDisable(true);
            }
        });

        ifComment.selectedProperty().addListener((observable, oldValue, newValue) -> {
            // 如果复选框选中，则将文本框设置为可编辑的状态
            if(newValue) {
                commentField.setEditable(true);
            }
            else {
                commentField.setEditable(false);
            }
        });

        file.setOnMousePressed(event -> file.setFitWidth(21));
        file.setOnMouseReleased(event -> {
            file.setFitWidth(23);
            List<File> filePaths = fileChooser.showOpenMultipleDialog(primaryStage);
            for(File f: filePaths) {
                String preText = srcs.getText();
                String fPath = f.getAbsolutePath();
                srcs.setText(preText + fPath + ";");
            }
        });
        dir.setOnMousePressed(event -> dir.setFitWidth(25));
        dir.setOnMouseReleased(event -> {
            dir.setFitWidth(27);
            File dirPath = directoryChooser.showDialog(primaryStage);
            String preText = srcs.getText();
            String dPath = dirPath.getAbsolutePath();
            srcs.setText(preText + dPath + ";");
        });

        desDir.setOnMousePressed(event -> desDir.setFitWidth(25));
        desDir.setOnMouseReleased(event -> {
            desDir.setFitWidth(27);
            File dirPath = directoryChooser.showDialog(primaryStage);
            String dPath = dirPath.getAbsolutePath();
            desPath.setText(dPath);
        });

        resetArchive.setOnMousePressed(event -> resetArchive.setFitWidth(29));
        resetArchive.setOnMouseReleased(event -> {
            resetArchive.setFitWidth(31);
            srcs.setText("");
            desPath.setText("");
        });

        startArchive.setOnAction(event -> {
            // 使用新的线程执行压缩，避免压缩时间过长造成阻塞
            new Thread(() -> {
                // 将进度条设置为可见
                archiveProgress.setVisible(true);

                String[] srcArray = srcs.getText().split(";");

                String archiveDesDir = desPath.getText();
                String archiveDesName = archiveName.getText();
                String suffix = archiveTypeSuffix.getText();
                String archiveDes = archiveDesDir + File.separator + archiveDesName + suffix;

                String commentValue = comment.getText();

                Archiver zipArchive = new Ziper(archiveProgress, unarchiveProgress);

                zipArchive.archive(srcArray, archiveDes, commentValue, (int) (archiveLevel.getValue()));

                // 将进度条设置为不可见
                archiveProgress.setVisible(false);
            }).start();
        });

        archivedFileIcon.setOnMousePressed(event -> archivedFileIcon.setFitWidth(23));
        archivedFileIcon.setOnMouseReleased(event -> {
            archivedFileIcon.setFitWidth(25);
            File srcArchived = fileChooser.showOpenDialog(primaryStage);
            zipSrc.setText(srcArchived.getAbsolutePath());
        });

        unarchiveDirIcon.setOnMousePressed(event -> unarchiveDirIcon.setFitWidth(25));
        unarchiveDirIcon.setOnMouseReleased(event -> {
            unarchiveDirIcon.setFitWidth(27);

            File desUnarchiveDir = directoryChooser.showDialog(primaryStage);
            unarchiveDesDir.setText(desUnarchiveDir.getAbsolutePath());
        });

        startUnarchive.setOnAction(event -> new Thread(() -> {
            unarchiveProgress.setVisible(true);

            String archivedSrc = zipSrc.getText();
            String unarchiveDir = unarchiveDesDir.getText();

            Archiver zipUnarchiver = new Ziper(archiveProgress, unarchiveProgress);

            zipUnarchiver.unArchive(archivedSrc, unarchiveDir);

            unarchiveProgress.setVisible(false);
        }).start());


        primaryStage.setResizable(false);
        primaryStage.setTitle("文件归档工具");
        primaryStage.setScene(new Scene(root, 398, 604));
        primaryStage.show();

    }
}
